<?php
/**
 * Created by PhpStorm.
 * User: s.utkin
 * Date: 21.04.2015
 * Time: 17:12
 */

namespace LogCollector;


use Pinenut\Model2\Finder;

class ActionStatisticsAction extends \Action {

	/**
	 * @inheritdoc
	 */
	protected function run()
	{
		$date = $this->request->date ? date("Y-m-d", strtotime($this->request->date)) : date("Y-m-d");
		$this->response->noCache(true);

		# TODO: Зарефакторить этот грязных хак, когда реализуем суммирование на уровне адаптера (нужно обсудить)
		$adapter = LogActionStatistics::getAdapter();
		$pipeline = [
			['$match' => [
				'time' => [
					'$gte' => strtotime(date("$date 00:00:00")),
					'$lte' => strtotime(date("$date 23:59:59")),
				],

			]],
			['$group' => [
				'_id' => '$action',
				'begins' => ['$sum' => '$startCount'],
				'ends' => ['$sum' => '$endCount'],
				'errors' => ['$sum' => '$errorCount'],
				'warns' => ['$sum' => '$warnCount'],
				'infos' => ['$sum' => '$infoCount'],
				'logs' => ['$sum' => '$logCount']
			]]

		];
		$this->response->date = $date;
		$this->response->items = $adapter->connect()->aggregate($pipeline)['result'];
	}

} 